package com.shop.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.Utils.ResponseMessage;
import com.shop.po.Product;
import com.shop.po.Shopcart;
import com.shop.po.User;
import com.shop.service.ProductService;
import com.shop.service.ShopcartService;


@Controller
public class cartController {
	@Autowired
	private ProductService productService;
	@Autowired
	private ShopcartService shopcartService;

	@RequestMapping("/addCart")
	public String addCart(HttpServletRequest request,@RequestParam int pid,@RequestParam int count ,Model model){
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		if (loginUser == null) {
			model.addAttribute("message","您还没有登录！");
			return "msg";
		}
		Product product = productService.finbProductByPid(pid);
		System.out.println(product);
		Shopcart scart=new Shopcart();
		scart.setUid(loginUser.getUid());
		scart.setPid(pid);
		scart.setCount(count);
		scart.setSubtotle(count*product.getShopPrice());
		scart.setPimage(product.getImage());
		scart.setPname(product.getPname());
		scart.setPprice(product.getShopPrice());
		scart.setProduct(product);
		Shopcart sc=shopcartService.selCartByUidAndPid(loginUser.getUid(), pid);
		if(sc!=null) {
			shopcartService.updateCount(count+sc.getCount(), sc.getId(),count*product.getShopPrice()+sc.getSubtotle());
		}
		else{
			shopcartService.insertCart(scart);
		}
		return "redirect:myCart.action";
	}
	
//	查看我的购物车
	@RequestMapping("/myCart")
	public String myCart(HttpServletRequest request,Model model){
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		if (loginUser == null) {
			model.addAttribute("message","您还没有登录！");
			return "msg";
		}
		List<Shopcart> cartlist=new ArrayList<>();
		cartlist=shopcartService.selCartByUid(loginUser.getUid());
		Double totale=0.0;
		for(Shopcart ss:cartlist) {
			totale+=ss.getSubtotle();
		}
		model.addAttribute("totale",totale);
		model.addAttribute("cartlist",cartlist);
		System.out.println(cartlist);
		return "cart";
	}
	
//	清空购物车
	@RequestMapping("/clearCart")
	public String clearCart(HttpServletRequest request){
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		shopcartService.deleteByUid(loginUser.getUid());
		return "redirect:myCart.action";
	}
//	删除某个购物项
	@RequestMapping("/delProFromCart")
	public String delProFromCart(@RequestParam int id,HttpServletRequest request){
		shopcartService.deleteByPrimaryKey(id);
		return "redirect:myCart.action";
	}
	//批量删除
	@RequestMapping("/deleteCarts.action")
	@ResponseBody
	public ResponseMessage deleteCarts(String deleteList)throws Exception{
		//因为传递的deleteList为带","的字符串,所以需要消掉分隔符转为数组
		String[] ids = deleteList.split(",");
		for(String id:ids){
			shopcartService.deleteByPrimaryKey(Integer.parseInt(id));
		}
		return ResponseMessage.success();
	}	
	
	@RequestMapping("/addCount.action")
	@ResponseBody
	public ResponseMessage addCount(HttpServletRequest request,@RequestParam int id)throws Exception{
		
		Shopcart sc=shopcartService.selCartById(id);
			int count=sc.getCount()+1;
			double subtotle=sc.getSubtotle()+sc.getPprice();
			shopcartService.updateCount(count, id, subtotle);
			return ResponseMessage.success();
		
	}	
	
	@RequestMapping("/subCount.action")
	@ResponseBody
	public ResponseMessage subCount(HttpServletRequest request,@RequestParam int id)throws Exception{
		Shopcart sc=shopcartService.selCartById(id);
		if(sc.getCount()!=1) {
			int count=sc.getCount()-1;
			double subtotle=sc.getSubtotle()-sc.getPprice();
			shopcartService.updateCount(count, id, subtotle);
			return ResponseMessage.success();
		}else {
			return ResponseMessage.error();
		}
	}	
	
	
}
